package com.swipemaster.levels.generator;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;

import com.google.gson.Gson;
import com.swipemaster.common.Coords;
import com.swipemaster.elements.Circle;
import com.swipemaster.levels.Level;
import com.swipemaster.levels.SurvivalLevel;
import com.swipemaster.moves.PlayerMove;

/**
 * Class to convert level objects into JSON and save them to file.
 * You should modify getLevel() to design your level.
 * When you are done, call generate() from main activity to save your level.
 */
public class LevelGenerator
{
	/**
	 * Convert level prepared in getLevel() to JSON and save it to file.
	 * 
	 * @param filename Name of your level file.
	 * @param activity When calling from activity, pass "this".
	 */
	public static void generate(String filename, Activity activity)
	{
		Level level = getLevel();
		String json = getJsonFromLevel(level);
		saveJsonToFile(json, filename, activity);
	}

	private static Level getLevel()
	{
		Circle player = new Circle(new Coords(0.5, 0.5), new PlayerMove(), Color.GREEN, 0.05);
		return new SurvivalLevel(player, 10);
	}
	
	private static String getJsonFromLevel(Level level)
	{
		Gson gson = new Gson();
		return gson.toJson(level);
	}
	
	private static void saveJsonToFile(String json, String filename, Activity activity)
	{
		try
		{
			FileOutputStream fos = activity.openFileOutput(filename, Context.MODE_PRIVATE);
			OutputStreamWriter out = new OutputStreamWriter(fos);
			out.write(json);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		} 
	}
}
